/**
 * Created by dimka3210 on 27.04.14.
 * Support by dimka3210@gmail.com
 */
(function (jQuery) {
    var $ = jQuery;

    var fSwitch = function (source) {
        var portal = $('.portal');
        var cat = $('.category');
        if (source == 'portal') {
            portal.show();
            cat.hide();
            setCookie('show_portal', 1, 365, '/');
        } else if (source == 'categories') {
            portal.hide();
            cat.show();
            setCookie('show_portal', 0, 365, '/');
        }
        fSetActiveClass(source);
    };

    var fSetActiveClass = function (source) {
        var links = $('#portal-switch').find('a');
        links.removeClass('active');
        links.each(function () {
            var self = $(this);
            if (self.attr('data') == source) {
                self.addClass('active');
                return false;
            }
        });
    };

    $(document).ready(function () {
        var pSwitch = $('#portal-switch');
        var links = pSwitch.find('a');
        var portalItems = $('.portal-item');
        links.click(function (e) {
            e.preventDefault();
            fSwitch($(this).attr('data'))
        });

        if (getCookie('show_portal') && getCookie('show_portal') == 1) {
            fSwitch('portal');
        } else {
            fSwitch('categories');
        }

        portalItems.each(function () {
            var self = $(this);
            var hint = self.find('.hint');
            var timeout = null;

            $(this).fancybox({
                href: $(this).find('.fancy-avatar').attr('href'),
                title: $(this).find('.fancy-avatar').attr('title')
            });

            hint.find('a').unbind('click').click(function () {
                window.location = $(this).attr('href');
            });

            self.mouseover(function () {
                hint.slideDown(300);
            }).mouseout(function () {
                timeout = setTimeout(function () {
                    hint.slideUp(100);
                }, 200);
            });

            hint.mouseover(function () {
                clearTimeout(timeout);
            });
        });
    });
})(jQuery);